package com.example.playnote.activity.login

import android.content.Intent
import android.os.Bundle
import android.text.TextUtils
import android.view.View
import android.widget.Toast
import com.example.playnote.MainActivity
import com.example.playnote.R
import com.example.playnote.base.BaseActivity
import com.example.playnote.bean.Constant
import com.example.playnote.bean.ToastFlag
import com.example.playnote.databinding.ActivityLoginBinding
import com.example.playnote.toasty
import com.example.playnote.utils.MMKVUtils
import com.example.playnote.viewModel.LoginViewModel
import com.gyf.immersionbar.ImmersionBar
import es.dmoral.toasty.Toasty


class LoginActivity : BaseActivity<LoginViewModel, ActivityLoginBinding>() {
    private val onClickListener = View.OnClickListener { view ->
        when(view.id) {
            R.id.login -> {
                if (checkContent()) {
                    mDatabind.loginProgress.visibility = View.VISIBLE
                    mViewModel.login(
                        mDatabind.username.text.toString(),
                        mDatabind.password.text.toString()
                    )
                }
            }
            R.id.register -> {
                if (checkContent()) {
                    mDatabind.loginProgress.visibility = View.VISIBLE
                    mViewModel.register(
                        mDatabind.username.text.toString(),
                        mDatabind.password.text.toString()
                    )
                }
            }
            R.id.loginExit -> {
                finish()
            }
        }
    }

    override fun initView(savedInstanceState: Bundle?) {
        mDatabind.login.setOnClickListener(onClickListener)
        mDatabind.register.setOnClickListener(onClickListener)
    }

    override fun createObserver() {
        super.createObserver()
        mViewModel.loginResult.observe(this) {
            if (it.errorMsg.isNotEmpty()) {
                this.toasty(ToastFlag.ERROR, it.errorMsg)
            } else {
                MMKVUtils.put(Constant.LOGIN_KEY, true)
                this.toasty(ToastFlag.SUCCESS, getString(R.string.welcome_back))
                val intent = Intent(this,MainActivity::class.java)
                startActivity(intent)
            }
            mDatabind.loginProgress.visibility = View.GONE
        }
        mViewModel.registerResult.observe(this) {
            if (it.errorMsg.isNotEmpty()) {
                Toasty.error(this, it.errorMsg, Toast.LENGTH_SHORT, true).show();
            } else {
                Toasty.success(this, "注册成功", Toast.LENGTH_SHORT, true).show();
            }
            mDatabind.loginProgress.visibility = View.GONE
        }
    }

    override fun initImmersionBar() {
        super.initImmersionBar()
        if (ImmersionBar.isSupportStatusBarDarkFont()) {
            immersionBar.statusBarDarkFont(true).init()
        }
    }

    private fun checkContent(): Boolean{
        mDatabind.username.error = null
        mDatabind.password.error = null
        var cancel = false
        var focusView: View? = null
        val pwd = mDatabind.password.text.toString()
        val userName = mDatabind.username.text.toString()
        // check password
        if (TextUtils.isEmpty(pwd)) {
            mDatabind.password.error = getString(R.string.password_not_empty)
            focusView = mDatabind.password
            cancel = true
        } else if (mDatabind.password.text.length < 6) {
            mDatabind.password.error = getString(R.string.password_length_short)
            focusView = mDatabind.password
            cancel = true
        }

        if (TextUtils.isEmpty(userName)) {
            mDatabind.username.error = getString(R.string.username_not_empty)
            focusView = mDatabind.username
            cancel = true
        }
        return if (cancel) {
            focusView?.let {
                it.requestFocus()
            }
            false
        } else {
            true
        }
    }

}